package com.lysj.bhp.wallet.tool;

import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.user.util.MyWallet;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.wallet.base.WalletContent;
import com.lysj.bhp.wallet.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.math.BigDecimal;

/**
 * @Author: ZhangWenJian
 * @Create: 2018/9/27--15:29
 * @Description:钱包工具类
 */
@Service
@Transactional
public class WalletTool extends WalletContent {
    @Resource
    MyWallet myWallet;
    //创建变量
    BitCoinWallet bitCoinWallet;
    EthereumWallet ethereumWallet;
    BhpcWallet bhpcWallet;
    DiamondBitcoinWallet diamondBitcoinWallet;
    BuWallet buWallet;
    /**
     * @author zwj
     * @date 2018-09-29 11:56
     * @@Description:查看指定钱包资产
     */
    public BigDecimal queryWalletMoney(Long userId,Integer walletType){
        BigDecimal walletMoney = BigDecimal.ZERO;
        switch (walletType){
            case 10:
                bitCoinWallet = bitcoinWalletRepository.findByUserId(userId);
                if(bitCoinWallet != null){
                    walletMoney = bitCoinWallet.getAvailBalance();
                }
                break;
            case 20:
                ethereumWallet = ethereumWalletRepository.findByUserId(userId);
                if(ethereumWallet != null){
                    walletMoney = ethereumWallet.getAvailBalance();
                }
                break;
            case 100:
                bhpcWallet = bhpcWalletRepository.findByUserId(userId);
                if(bhpcWallet != null){
                    walletMoney = bhpcWallet.getAvailBalance();
                }
                break;
            case 30:
                diamondBitcoinWallet = diamondBitcoinWalletRepository.findByUserId(userId);
                if(diamondBitcoinWallet != null){
                    walletMoney = diamondBitcoinWallet.getAvailBalance();
                }
                break;
            case 40:
                buWallet = buWalletRepository.findByUserId(userId);
                if(buWallet != null){
                    walletMoney = buWallet.getAvailBalance();
                }
                break;
        }
        return walletMoney;
    }
    /**
     * @author zwj
     * @date 2018-09-29 14:30
     * @@Description:修改钱包资产 optionType: 1.增加 2.减少
     */
    public Resp alterWalletBalance(Long userId,Integer walletType,BigDecimal walletMoney,Integer optionType) throws Exception {
        BigDecimal realityMoney = walletMoney;
        if(optionType == 2){
            walletMoney = walletMoney.multiply(new BigDecimal("-1"));
        }
        switch (walletType){
            case 10:
                bitCoinWallet = bitcoinWalletRepository.findByUserId(userId);
                if(bitCoinWallet == null){
                    bitCoinWallet = (BitCoinWallet)setUpUserWallet(walletType,userId).getObj();
                }
                if(optionType == 2){
                    if(bitCoinWallet.getAvailBalance().compareTo(realityMoney) == -1){
                        return new Resp(Resp.PARAM_ERROR,"操作失败,钱包资产不足!");
                    }
                }
                bitCoinWallet.setBalance(bitCoinWallet.getBalance().add(walletMoney));
                bitCoinWallet.setAvailBalance(bitCoinWallet.getAvailBalance().add(walletMoney));
                break;
            case 20:
                ethereumWallet = ethereumWalletRepository.findByUserId(userId);
                if(ethereumWallet == null){
                    ethereumWallet = (EthereumWallet)setUpUserWallet(walletType,userId).getObj();
                }
                if(optionType == 2){
                    if(ethereumWallet.getAvailBalance().compareTo(realityMoney) == -1){
                        return new Resp(Resp.PARAM_ERROR,"操作失败,钱包资产不足!");
                    }
                }
                ethereumWallet.setBalance(ethereumWallet.getBalance().add(walletMoney));
                ethereumWallet.setAvailBalance(ethereumWallet.getAvailBalance().add(walletMoney));
                break;
            case 100:
                bhpcWallet = bhpcWalletRepository.findByUserId(userId);
                if(bhpcWallet == null){
                    bhpcWallet = (BhpcWallet)setUpUserWallet(walletType,userId).getObj();
                }
                if(optionType == 2){
                    if(bhpcWallet.getAvailBalance().compareTo(realityMoney) == -1){
                        return new Resp(Resp.PARAM_ERROR,"操作失败,钱包资产不足!");
                    }
                }
                bhpcWallet.setBalance(bhpcWallet.getBalance().add(walletMoney));
                bhpcWallet.setAvailBalance(bhpcWallet.getAvailBalance().add(walletMoney));
                break;
            case 30:
                diamondBitcoinWallet = diamondBitcoinWalletRepository.findByUserId(userId);
                if(diamondBitcoinWallet == null){
                    diamondBitcoinWallet = (DiamondBitcoinWallet)setUpUserWallet(walletType,userId).getObj();
                }
                if(optionType == 2){
                    if(diamondBitcoinWallet.getAvailBalance().compareTo(realityMoney) == -1){
                        return new Resp(Resp.PARAM_ERROR,"操作失败,钱包资产不足!");
                    }
                }
                diamondBitcoinWallet.setBalance(diamondBitcoinWallet.getBalance().add(walletMoney));
                diamondBitcoinWallet.setAvailBalance(diamondBitcoinWallet.getAvailBalance().add(walletMoney));
                break;
            case 40:
                buWallet = buWalletRepository.findByUserId(userId);
                if(buWallet == null){
                    buWallet = (BuWallet)setUpUserWallet(walletType,userId).getObj();
                }
                if(optionType == 2){
                    if(buWallet.getAvailBalance().compareTo(realityMoney) == -1){
                        return new Resp(Resp.PARAM_ERROR,"操作失败,钱包资产不足!");
                    }
                }
                buWallet.setBalance(buWallet.getBalance().add(walletMoney));
                buWallet.setAvailBalance(buWallet.getAvailBalance().add(walletMoney));
                break;
        }
        return new Resp("修改钱包资产成功!");
    }
    /**
     * @author zwj
     * @date 2018-09-29 14:50
     * @@Description:创建钱包
     */
    public Resp setUpUserWallet(Integer walletType,Long userId) throws Exception {
        Resp resp = new Resp();
        User user = userContent.userRepository.findOne(userId);
        switch (walletType){
            case 10:
                resp = myWallet.createBitWallet(userId,user.getEmail());
                break;
            case 20:
                resp = myWallet.createEthWallet(userId);
                break;
            case 100:
                resp = new Resp(myWallet.createBhpcWallet(userId));
                break;
            case 30:
                resp = new Resp(diamonBitcoinWalletService.createDiamondBitcoinWallet(userId));
                break;
            case 40:
                resp = new Resp(buWalletService.createBuWallet(userId));
                break;
        }
        return resp;
    }










}
